// Thud - a dynamic object-oriented language
// Copyright (C) 2009,2010 David H. Hovemeyer <david.hovemeyer@gmail.com>

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

/*
 * ParserContext.cpp
 *
 *  Created on: Nov 24, 2009
 *      Author: dhovemey
 */

#include "util/FileInputStream.h"
#include "util/InputStreamReader.h"
//#include "fe/Token.h"
#include "fe/Token2.h"
#include "fe/Lexer2.h"
#include "fe/ASTNode.h"
#include "fe/yyparse.h"
#include "fe/Parser.h"
#include "fe/ParserFunctions.h"

Parser::Parser()
	: m_scanner(0)
//	, m_prevToken(0)
//	, m_firstToken(0)
	, m_ast(0)
{
}

Parser::~Parser()
{
#if 0
	yylex_destroy(m_scanner);

	// delete tokens
	for (Token *t = m_firstToken; t != 0; ) {
		Token *next = t->getNext();
		delete t;
		t = next;
	}
#endif
}

#if 0
void Parser::initialize(FILE *in)
{
	yylex_init(&m_scanner);
	yyset_in(in, m_scanner);

	// Make the ParserContext object available from the lexer
	yyset_extra(this, m_scanner);

	// The lexer will build a linked list of Tokens
	m_prevToken = 0;
	Reader *reader = new InputStreamReader(new FileInputStream(in));
	m_scanner = new Lexer2(reader);
}
#endif

void Parser::initialize(Reader *readerToAdopt)
{
	m_scanner = new Lexer2(readerToAdopt);
}

void Parser::parse()
{
	yyparse(this);
}

ASTNode *Parser::getAST()
{
	return m_ast;
}

void Parser::setAST(ASTNode *ast)
{
	m_ast = ast;
}

Lexer2 *Parser::getScanner()
{
	return m_scanner;
}

//void ParserContext::setPrevToken(Token *prevToken)
//{
//	m_prevToken = prevToken;
//	if (m_firstToken == 0) {
//		m_firstToken = prevToken;
//	}
//}
